using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using GeoFramework.Gps;
using GeoFramework.Gps.Nmea;

namespace GeoFramework.Examples
{
    public partial class UnderstandingPrecisionForm : Form
    {
        public UnderstandingPrecisionForm()
        {
            InitializeComponent();

            // Start receiving GPS data immediately
            GpsDevice.Start();

            // Set the maximum horizontal dilution of precision
            GpsDevice.MaximumHorizontalDilutionOfPrecision = new DilutionOfPrecision(12);

            // Set the track bar to the current HDOP rating (or 20, whichever is less)
            CurrentHorizontalDilutionOfPrecisionTrackBar.Value = (int)Math.Min(GpsDevice.HorizontalDilutionOfPrecision.Value, 20);

        }

        private void GpsDevice_HorizontalDilutionOfPrecisionChanged(object sender, DilutionOfPrecisionEventArgs e)
        {
            // Update the slider bar to the current HDOP value  (up to 20)
            CurrentHorizontalDilutionOfPrecisionTrackBar.Value = (int)Math.Min(e.DilutionOfPrecision.Value, 20);

            // Also update the label to make it clear when readings are being accepted
            // versus being ignored.
            if (CurrentHorizontalDilutionOfPrecisionTrackBar.Value <= MaximumDilutionOfPrecisionTrackBar.Value)
            {
                PrecisionStatusLabel.Text = "Precision is currently good enough to accept positional readings.";
                PrecisionStatusLabel.ForeColor = Color.Green;
            }
            else
            {
                PrecisionStatusLabel.Text = "Precision is currently not good enough.  Positional readings are being ignored.";
                PrecisionStatusLabel.ForeColor = Color.Red;
            }
        }

        private void MaximumDilutionOfPrecisionTrackBar_Scroll(object sender, System.EventArgs e)
        {
            // Set the maximum HDOP allowed in positional readings
            GpsDevice.MaximumHorizontalDilutionOfPrecision =
                new DilutionOfPrecision(MaximumDilutionOfPrecisionTrackBar.Value);

            // Also update the label to make it clear when readings are being accepted
            // versus being ignored.
            if (CurrentHorizontalDilutionOfPrecisionTrackBar.Value <= MaximumDilutionOfPrecisionTrackBar.Value)
            {
                PrecisionStatusLabel.Text = "Precision is currently good enough to accept positional readings.";
                PrecisionStatusLabel.ForeColor = Color.Green;
            }
            else
            {
                PrecisionStatusLabel.Text = "Precision is currently not good enough.  Positional readings are being ignored.";
                PrecisionStatusLabel.ForeColor = Color.Red;
            }
        }

        private void GpsDevice_PositionChanged(object sender, GeoFramework.PositionEventArgs e)
        {
            /* When precision is being enforced, this event will only fire when precision
             * is good enough to accept positional readings.
             */

            PositionLabel.Text = e.Position.ToString() + " at " + e.PositionTimeStamp.ToLocalTime().ToLongTimeString();
        }
    }
}